package com.example.admin.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysDictType;
import com.example.admin.service.ISysDictTypeService;
import com.example.shrio.CurrentUser;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.page.Pager;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 * 前端控制器 统一参数类型
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@RestController
@Api(value = "字典管理Controller", tags = {"字典管理操作接口"})
@RequestMapping("/sys/sysDictType")
public class SysDictTypeController {

    @Autowired
    private ISysDictTypeService service;

    /**
     * 获取参数类型
     *
     * @param pager
     * @return
     */
    @ApiOperation(value = "字典管理列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysDictType> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        pager.convertPage(service.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }

    @ApiOperation(value = "字典管理列表", httpMethod = "GET")
    @GetMapping
    public AjaxResult list() {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<SysDictType>();
        queryWrapper.orderByAsc("id");
        queryWrapper.eq("status", 0);
        return new AjaxResult(APIState.S00, service.list(queryWrapper));
    }

    /**
     * 保存参数
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "字典管理新增", httpMethod = "POST")
    @PostMapping
    public AjaxResult save(@RequestBody SysDictType type) {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<SysDictType>();
        queryWrapper.eq("dict_type", type.getDictType());
        SysDictType dictType = service.getOne(queryWrapper);
        if (dictType != null) {
            return new AjaxResult(APIState.S01, "该类型已存在");
        }
        if (type != null) {
            type.setStatus(0);
            type.setCreateTime(new Date());
            type.setCreateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (type.insert()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 修改
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "字典管理编辑", httpMethod = "PUT")
    @PutMapping
    public AjaxResult update(@RequestBody SysDictType type) {
        if (type != null && type.getId() != null) {
            type.setUpdateTime(new Date());
            type.setUpdateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (type.insertOrUpdate()) {
                return new AjaxResult(APIState.S00, "保存成功");
            }
        }
        return new AjaxResult(APIState.S01, "保存失败");
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "字典管理删除", httpMethod = "DELETE")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        if (service.deleteType(Arrays.asList(ids))) {
            return new AjaxResult(APIState.S00, "删除成功");
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }

    @ApiOperation(value = "字典管理启用/停用", httpMethod = "PUT")
    @PutMapping("start")
    public AjaxResult start(Integer id) {
        SysDictType type = service.getById(id);
        if (type != null) {
            if (type.getStatus() == 1) {
                type.setStatus(0);
            } else {
                type.setStatus(1);
            }
            type.setUpdateTime(new Date());
            type.setUpdateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
            if (service.updateById(type)) {
                return new AjaxResult(APIState.S00, "更改成功");
            }
        }
        return new AjaxResult(APIState.S01, "更改失败");
    }

}
